package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.inventory.model.Batch;
import com.fadadianqi.logistics.inventory.model.Item;
import com.fadadianqi.logistics.inventory.service.BatchService;
import com.fadadianqi.logistics.inventory.service.InventoryService;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 库存退回给供货商
 *
 * @author aroot
 */
@Controller
public class InventoryReturnController {

    private static final String PARAM_BACK_URL = "backURL";

    /**
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/a/batch/return", method = RequestMethod.GET)
    public String showForm(HttpServletRequest reuqest, ModelMap modelMap) {
        String batchId = ParamUtils.getString(reuqest, "batchId", null);
        Batch batch = batchService.getById(batchId);
        Item item = inventoryService.getNormalItemByBatchId(batchId);
        if (item == null) {
            return "redirect:/a/batch/list?categoryId="
                    + batch.getCategoryId()
                    + "&brandId=" + batch.getBrandId();
        }

        // 当前批次可退回(未售出)的货物数量
        int count = inventoryService.countNormalStatusByBatchId(batchId);
        modelMap.put("count", count);
        modelMap.put("goods", goodsService.getGoodsById(batch.getGoodsId()));
        modelMap.put("brand", goodsService.getBrandById(batch.getBrandId()));
        modelMap.put("category", goodsService.getCategoryById(batch.getCategoryId()));
        modelMap.put("batch", batch);
        modelMap.put("item", item);
        modelMap.put("brandList", getBrandList(null));
        modelMap.put("categoryList", getCategoryList(null));
        return "/admin/batch/return";
    }

    @RequestMapping(value = "/a/batch/return", method = RequestMethod.POST)
    public String doReturn(HttpServletRequest reuqest, ModelMap modelMap) {
        String batchId = ParamUtils.getString(reuqest, "batchId", null);
        Batch batch = batchService.getById(batchId);
        int count = ParamUtils.getInt(reuqest, "count", 1);
        for (int i = 0; i < count; i++) {
            Item item = inventoryService.getNormalItemByGoodsId(batch.getGoodsId());
            if (item != null) {
                item.setStatus(Item.STATUS_RETURN);
                inventoryService.update(item);
            }
        }
        String backURL = ParamUtils.getString(reuqest, PARAM_BACK_URL, null);
        if (ValidatorHelper.isNotEmpty(backURL)) {
            return "redirect:" + backURL;
        } else {
            return "redirect:/a/batch/list?categoryId="
                    + batch.getCategoryId()
                    + "&brandId=" + batch.getBrandId();
        }
    }

    private List<Category> getCategoryList(String keys) {
        List<Category> categoryList = goodsService.findAllCategory();
        return categoryList;
    }

    private List<Brand> getBrandList(String keys) {
        List<Brand> brandList = goodsService.findAllBrand();
        return brandList;
    }

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private BatchService batchService;
}
